package com.hk.core.autoconfigure.exception;

import com.hk.commons.util.JsonUtils;
import com.hk.core.autoconfigure.exception.servlet.AbstractExceptionHandler;
import com.hk.core.web.Webs;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import java.lang.reflect.Method;

/**
 * @author Kevin
 */
@RequiredArgsConstructor
public class CustomAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {

    private final UncaughtExceptionConsumer uncaughtExceptionConsumer;

    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        var exceptionDetails = AbstractExceptionHandler.createExceptionDetails(ex, null, Webs.getHttpServletRequest());
        exceptionDetails.setAsync(Boolean.TRUE)
                .setInvokeMethodName(method.getName())
                .setInvokeMethodArgs(JsonUtils.serialize(params));
        uncaughtExceptionConsumer.accept(exceptionDetails, ex);
    }
}
